﻿#pragma once
#include <graphics.h>
#include <string>
#include <functional>
#include <algorithm>
#include "Material.h"  // 确保统一的枚举定义

//class GUI：实现了图形用户界面
class GUI {
    Material selectedMat = SAND;
    std::function<void()> clearCallback;

    // 面板和按钮参数
    const int PANEL_X = 10;
    const int PANEL_Y = 10;
    const int BUTTON_WIDTH = 150;
    const int BUTTON_HEIGHT = 40;
    const int CLEAR_BTN_WIDTH = 70;
    const int CLEAR_BTN_HEIGHT = 30;
    const int TOTAL_MATERIALS = 7;  // 材料总数为7种（原4种+新增3种）
    // 新的清空按钮位置偏移量
    const int NEW_CLEAR_BTN_OFFSET_X = 170;
    const int NEW_CLEAR_BTN_OFFSET_Y = 10;

public:
    //setClearCallback：设置清除屏幕的回调函数
    void setClearCallback(std::function<void()> cb) { clearCallback = cb; }
    //setSelectedMat 和 getSelectedMat：设置和获取当前选择的物质类型
    void setSelectedMat(Material mat) { selectedMat = mat; }
    Material getSelectedMat() const { return selectedMat; }

    //handleInput：处理鼠标输入，包括选择物质类型和清除屏幕
    void handleInput(const MOUSEMSG& msg) {
        if (msg.uMsg == WM_LBUTTONDOWN) {
            // 处理选择按钮点击事件（扩展到7个按钮）
            if (isInRect(msg.x, msg.y,
                PANEL_X, PANEL_Y,
                PANEL_X + BUTTON_WIDTH,
                PANEL_Y + TOTAL_MATERIALS * BUTTON_HEIGHT))
            {
                int buttonIndex = (msg.y - PANEL_Y) / BUTTON_HEIGHT;
                if (buttonIndex >= 0 && buttonIndex < TOTAL_MATERIALS) {
                    selectedMat = static_cast<Material>(buttonIndex);
                }
            }

            // 处理清除按钮事件（位置更改）
            int clearBtnX = PANEL_X + NEW_CLEAR_BTN_OFFSET_X;
            int clearBtnY = PANEL_Y + NEW_CLEAR_BTN_OFFSET_Y;
            if (isInRect(msg.x, msg.y,
                clearBtnX, clearBtnY,
                clearBtnX + CLEAR_BTN_WIDTH,
                clearBtnY + CLEAR_BTN_HEIGHT))
            {
                if (clearCallback) clearCallback();
            }
        }
    }

    //draw：绘制界面，包括物质选择按钮、清除按钮和信息面板
    void draw(int brushSize) const {
        setlinestyle(PS_SOLID, 2);
        settextstyle(16, 0, _T("宋体"));

        // 绘制面板框架（高度扩展到7个按钮）
        drawPanelFrame();
        drawMaterialButton(PANEL_Y + 0 * BUTTON_HEIGHT, SAND);
        drawMaterialButton(PANEL_Y + 1 * BUTTON_HEIGHT, WATER);
        drawMaterialButton(PANEL_Y + 2 * BUTTON_HEIGHT, FIRE);
        drawMaterialButton(PANEL_Y + 3 * BUTTON_HEIGHT, STONE);
        drawMaterialButton(PANEL_Y + 4 * BUTTON_HEIGHT, GAS);   // 新增气体
        drawMaterialButton(PANEL_Y + 5 * BUTTON_HEIGHT, ICE);   // 新增冰块
        drawMaterialButton(PANEL_Y + 6 * BUTTON_HEIGHT, OIL);   // 新增油

        // 绘制清除按钮信息面板（位置更改）
        drawClearButton();
        drawInfoPanel(brushSize);
    }

    //isMouseOverUI：检查鼠标是否悬停在界面上
    bool isMouseOverUI(int x, int y) const {
        int clearBtnX = PANEL_X + NEW_CLEAR_BTN_OFFSET_X;
        int clearBtnY = PANEL_Y + NEW_CLEAR_BTN_OFFSET_Y;
        return isInRect(x, y,
            PANEL_X, PANEL_Y,
            PANEL_X + BUTTON_WIDTH,
            PANEL_Y + TOTAL_MATERIALS * BUTTON_HEIGHT + 40)
            || isInRect(x, y,
                clearBtnX, clearBtnY,
                clearBtnX + CLEAR_BTN_WIDTH,
                clearBtnY + CLEAR_BTN_HEIGHT);
    }

private:
    void drawPanelFrame() const {
        setfillcolor(RGB(240, 240, 240));
        solidrectangle(PANEL_X, PANEL_Y,
            PANEL_X + BUTTON_WIDTH,
            PANEL_Y + TOTAL_MATERIALS * BUTTON_HEIGHT);
    }

    void drawMaterialButton(int yPos, Material mat) const {
        const char* names[] = {
            "沙子 (1)", "水 (2)", "火 (3)", "石头 (4)",
            "气体 (5)", "冰块 (6)", "油 (7)"  // 新增材料名称
        };

        // 颜色定义与 Material.h 中的枚举对应
        const COLORREF colors[] = {
            MATERIAL_COLORS[SAND],   // 0
            MATERIAL_COLORS[WATER],   // 1
            MATERIAL_COLORS[FIRE],    // 2
            MATERIAL_COLORS[STONE],   // 3
            MATERIAL_COLORS[GAS],     // 4 气体
            MATERIAL_COLORS[ICE],     // 5 冰块
            MATERIAL_COLORS[OIL]     // 6 油
        };

        // 按钮背景颜色（选中时高亮）
        bool isSelected = (mat == selectedMat);
        setfillcolor(isSelected ? colors[mat] : RGB(220, 220, 220));
        solidrectangle(PANEL_X, yPos,
            PANEL_X + BUTTON_WIDTH,
            yPos + BUTTON_HEIGHT);

        // 绘制预览圆
        setfillcolor(colors[mat]);
        solidcircle(PANEL_X + 25, yPos + BUTTON_HEIGHT / 2, 10);

        // 绘制按钮标签（居中显示）
        settextcolor(isSelected ? WHITE : BLACK);
        outtextxy(PANEL_X + 45, yPos + 12, names[mat]);
    }

    void drawClearButton() const {
        // 清空按钮位置更改
        int clearBtnX = PANEL_X + NEW_CLEAR_BTN_OFFSET_X;
        int clearBtnY = PANEL_Y + NEW_CLEAR_BTN_OFFSET_Y;
        setfillcolor(RGB(255, 100, 100));
        solidrectangle(clearBtnX, clearBtnY,
            clearBtnX + CLEAR_BTN_WIDTH,
            clearBtnY + CLEAR_BTN_HEIGHT);

        settextcolor(WHITE);
        outtextxy(clearBtnX + 10, clearBtnY + 5, "清空画布");
    }

    void drawInfoPanel(int brushSize) const {
        // 信息面板位置微调
        int infoY = PANEL_Y + TOTAL_MATERIALS * BUTTON_HEIGHT + 50;
        settextcolor(BLACK);
        char buffer[64];
        sprintf_s(buffer, "当前材料: %s", getMatName().c_str());
        outtextxy(PANEL_X, infoY, buffer);

        sprintf_s(buffer, "画笔大小: %d", brushSize);
        outtextxy(PANEL_X, infoY + 20, buffer);
    }

    std::string getMatName() const {
        switch (selectedMat) {
        case SAND:  return "沙子";
        case WATER: return "水";
        case FIRE:  return "火";
        case STONE: return "石头";
        case GAS:   return "气体";  // 新增
        case ICE:   return "冰块";    // 新增
        case OIL:   return "油";    // 新增
        default:    return "未知";
        }
    }

    bool isInRect(int x, int y, int x1, int y1, int x2, int y2) const {
        return (x >= x1) && (x <= x2) && (y >= y1) && (y <= y2);
    }
};